import MonsterModel from 'server/model/monsterModel';
import {
    totalConfig,
    propName
} from 'server/config/templateConfig';
import FightAttri from 'server/model/fightAttri';
import utils from 'server/core/utils';
let monstersConfig = totalConfig.monster;
let montserAttrisConfig = totalConfig.other[290001].monsterTypes;

//1前排2中间3后排
let monsterPos = [
    [1, 2, 3, 4],
    [1, 2, 3, 4, 5, 6],
    [3, 4, 5, 6, 7, 8, 9]
];
class MonsterHandle {
    static getInstance() {
        if (!MonsterHandle.instance) {
            MonsterHandle.instance = new MonsterHandle();
        }
        return MonsterHandle.instance;
    }

    createMonster(monsterId) {
        let monsterInfo = monsterConfig[monsterId];
        let monster = new MonsterModel(monsterInfo);
        return monster;
    }

    createMonsters(monsterId, lvl) {
        //type=1前排2中间3后排
        let monsters = [];
        lvl = lvl || 1;
        let monsterConfig = monstersConfig[monsterId];
        for (let i in monsterPos) {
            let index = utils.getOneFromArray(monsterPos[i]);
            let montserAttriConfig = montserAttrisConfig[index];
            let monster = new MonsterModel({
                name: monsterConfig.name + montserAttriConfig.name,
                fightAttri: {
                    maxHp: montserAttriConfig.maxHp[0] * lvl + montserAttriConfig.maxHp[1] + monsterConfig.maxHp[0] * lvl + monsterConfig.maxHp[1], //生命
                    attack: montserAttriConfig.attack[0] * lvl + montserAttriConfig.attack[1] + monsterConfig.attack[0] * lvl + monsterConfig.attack[1], //攻击力
                    pdef: montserAttriConfig.pdef[0] * lvl + montserAttriConfig.pdef[1] + monsterConfig.pdef[0] * lvl + monsterConfig.pdef[1], //物防
                    ldef: montserAttriConfig.ldef[0] * lvl + montserAttriConfig.ldef[1] + monsterConfig.ldef[0] * lvl + monsterConfig.ldef[1], //法防
                    toughness: montserAttriConfig.toughness[0] * lvl + montserAttriConfig.toughness[1] + monsterConfig.toughness[0] * lvl + monsterConfig.toughness[1],
                    guard: montserAttriConfig.guard[0] * lvl + montserAttriConfig.guard[1] + monsterConfig.guard[0] * lvl + monsterConfig.guard[1],
                    recover: montserAttriConfig.recover[0] * lvl + montserAttriConfig.recover[1] + monsterConfig.recover[0] * lvl + monsterConfig.recover[1],
                    crit: montserAttriConfig.crit[0] * lvl + montserAttriConfig.crit[1] + monsterConfig.crit[0] * lvl + monsterConfig.crit[1],
                    speed: montserAttriConfig.speed[0] * lvl + montserAttriConfig.speed[1] + monsterConfig.speed[0] * lvl + monsterConfig.speed[1],
                    critHurt: montserAttriConfig.critHurt[0] * lvl + montserAttriConfig.critHurt[1] + monsterConfig.critHurt[0] * lvl + monsterConfig.critHurt[1],
                    lvl: lvl,
                }
            });
            monsters.push(monster);
        }
        return monsters;
    }

    createMonster(monsterId, monsterType, lvl) {
        let monsterConfig = monstersConfig[monsterId];
        let monsters = {};
        for (let [monsterType, montserAttriConfig] of Object.entries(montserAttrisConfig)) {
            let monster = new MonsterModel({
                name: monsterConfig.name + montserAttriConfig.name,
                fightAttri: {
                    maxHp: montserAttriConfig.maxHp[0] * lvl + montserAttriConfig.maxHp[1] + monsterConfig.maxHp[0] * lvl + monsterConfig.maxHp[1], //生命
                    attack: montserAttriConfig.attack[0] * lvl + montserAttriConfig.attack[1] + monsterConfig.attack[0] * lvl + monsterConfig.attack[1], //攻击力
                    pdef: montserAttriConfig.pdef[0] * lvl + montserAttriConfig.pdef[1] + monsterConfig.pdef[0] * lvl + monsterConfig.pdef[1], //物防
                    ldef: montserAttriConfig.ldef[0] * lvl + montserAttriConfig.ldef[1] + monsterConfig.ldef[0] * lvl + monsterConfig.ldef[1], //法防
                    toughness: montserAttriConfig.toughness[0] * lvl + montserAttriConfig.toughness[1] + monsterConfig.toughness[0] * lvl + monsterConfig.toughness[1],
                    guard: montserAttriConfig.guard[0] * lvl + montserAttriConfig.guard[1] + monsterConfig.guard[0] * lvl + monsterConfig.guard[1],
                    recover: montserAttriConfig.recover[0] * lvl + montserAttriConfig.recover[1] + monsterConfig.recover[0] * lvl + monsterConfig.recover[1],
                    crit: montserAttriConfig.crit[0] * lvl + montserAttriConfig.crit[1] + monsterConfig.crit[0] * lvl + monsterConfig.crit[1],
                    speed: montserAttriConfig.speed[0] * lvl + montserAttriConfig.speed[1] + monsterConfig.speed[0] * lvl + monsterConfig.speed[1],
                    critHurt: montserAttriConfig.critHurt[0] * lvl + montserAttriConfig.critHurt[1] + monsterConfig.critHurt[0] * lvl + monsterConfig.critHurt[1],
                    lvl: lvl,
                }
            });
            monsters[monsterType] = monster
        }

        return monsters;
    }
    getFightAttri(monster) {
        return monster.fightAttri;
    }

    getFightAttriCopy(monster) {
        return FightAttri.create(monster.fightAttri);
    }

}

let monsterHandle = MonsterHandle.getInstance();
export default monsterHandle;